﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Xml.Linq;
using DAL;

public partial class admin_Media_Video_Edit : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      Session["url_back_admin"] = Request.RawUrl;
      LoadTitle();
      LoadPosition();
      if (Request.QueryString["Id"] != null)
        Filldata();
    }
  }

  VideoBLL videoData = new VideoBLL();
  private void LoadPosition()
  {
    dropPosition.Items.Clear();
    XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.xml");
    var xPosition = xElement.Descendants("Videos").Descendants("Position");
    foreach (XElement item in xPosition)
    {
      dropPosition.Items.Add(new ListItem(item.Element("Name").Value, item.Attribute("Id").Value));
    }
  }
  private void Filldata()
  {
    int id = Convert.ToInt32(Request.QueryString["Id"]);
    Video video = videoData.GetById(id);
    txtName.Text = video.Name;
    dropPosition.SelectedValue = video.Position.ToString();
    if (video.isEnable == true) radioEnable.Checked = true;
    else radioVisible.Checked = true;
  }
  private void LoadTitle()
  {
    if (Request.QueryString["Id"] != null)
    {
      h3Title.InnerText = "Chỉnh sửa Video";
    }
  }
  private bool isValidate()
  {
      if (rdVideoComputer.Checked)
      {
          if (!fileUpload.HasFile && Request.QueryString["Id"] == null)
          {
              Myfunction.ThongBao(lbThongBao, "Chọn file Video");
              return false;
          }
      }
    return true;
  }
  private void ResetForm()
  {
    radioEnable.Checked = true;
    txtName.Text = "";
  }
  protected void btnOk_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      Myfunction.ThongBao(lbThongBao, "Bạn không có quyền thực hiện thao tác này ! ");
      return;
    }
    try
    {
      lbThongBao.Text = "";
      if (isValidate())
      {
        if (Request.QueryString["Id"] == null)
        {
          Add();
          ResetForm();
        }
        else
          Update();


      }
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  }
  private void Update()
  {
    int id = Convert.ToInt32(Request.QueryString["Id"]);
    Video video = videoData.GetById(id);
    video.Name = txtName.Text;
    video.Position = Convert.ToInt32(dropPosition.SelectedValue);

    if (rdVideoComputer.Checked)
    {
        video.isYoutube = false;
        if (fileUpload.HasFile)
        {
            if (video.Link == "" || video.Link == null)
                video.Link = Myfunction.CopyFile(fileUpload.PostedFile, "Media/Video");
            else
                video.Link = Myfunction.UpdateFile(fileUpload.PostedFile, "Media/Video", video.Link);
        }
    }
    else if (rdYoutube.Checked)
    {
        video.isYoutube = true;
        video.Link = txtFile.Text;
    }
    video.isEnable = (radioEnable.Checked ? true : false);

    videoData.Update(video);

    Myfunction.ThongBao(lbThongBao, "Video đã được cập nhật");
  }
  private void Add()
  {
    Video video = new Video();
    video.isEnable = (radioEnable.Checked ? true : false);
    video.Name = txtName.Text;
    video.Position = Convert.ToInt32(dropPosition.SelectedValue);

    if (rdVideoComputer.Checked)
    {
        video.isYoutube = false;
        if (fileUpload.HasFile)
        {
            video.Link = Myfunction.CopyFile(fileUpload.PostedFile, "Media/Video");
        }
    }
    else if (rdYoutube.Checked)
    {
        video.isYoutube = true;
        video.Link = txtFile.Text;
    }
    videoData.Add(video);
    Myfunction.ThongBao(lbThongBao, "Video đã được cập nhật");
  }
  protected void rdYoutube_CheckedChanged(object sender, EventArgs e)
  {
      if (rdYoutube.Checked)
      {
          txtFile.Visible = true;
          fileUpload.Visible = false;
      }
      else
      {
          txtFile.Visible = false;
          fileUpload.Visible = true;
      }
  }
}